import java.util.ArrayList;
import java.util.Scanner;
import java.util.zip.DeflaterOutputStream;

public class StudentSystem {
    public static void main(String[] args) {
        //创建容器
        ArrayList<student> list = new ArrayList<>();
        loop:
        while (true) {
            System.out.println("-------欢迎来到黑马学生管理系统-------");
            System.out.println("1 添加学生");
            System.out.println("2 删除学生");
            System.out.println("3 查询学生");
            System.out.println("4 修改学生");
            System.out.println("5 退出系统 ");
            System.out.println("请输入您的选择");
            Scanner sc = new Scanner(System.in);
            int choose = sc.nextInt();
            switch (choose) {

                case 1:
                    Addstudet(list);
                    break;
                case 2:
                    Deletestudet(list);
                    break;
                case 3:
                    Searchstudet(list);
                    break;
                case 4:
                    Updatestudet(list);
                    break;
                case 5:
                    System.out.println("5 退出 ");
                    //break loop;
                    //退出的第二种写法
                    //停止Java的虚拟机运行
                    System.exit(0);

                default:
                    System.out.println("输入错误，请重新输入");
                    break;
            }
        }
    }

    //添加学生的业务逻辑
    public static void Addstudet(ArrayList<student> list) {
        Scanner sc = new Scanner(System.in);
        student s = new student();
        while(true){
            System.out.println("请输入学生id");
            String id = sc.next();
            //进行判断
            boolean flag=contions(list,id);
            if(flag){
                //id已经存在 需要重新录入
                System.out.println("id存在，请重新录入");
            }else{
                //添加
                s.setId(id);
                break;
            }
        }

        System.out.println("请输入学生姓名");
        String name = sc.next();
        s.setName(name);

        System.out.println("请输入学生年龄");
        int age = sc.nextInt();
        s.setAge(age);

        System.out.println("请输入学生住址");
        String address = sc.next();
        s.setAddress(address);


        //添加到容器里面去
        list.add(s);

        //提示用户
        System.out.println("学生信息添加成功");
    }

    //删除学生
    public static void Deletestudet(ArrayList<student> list) {
        System.out.println("删除学生");
    }

    //修改学生
    public static void Updatestudet(ArrayList<student> list) {
        System.out.println("修改学生");
    }

    //查找学生
    public static void Searchstudet(ArrayList<student> list) {
        //没有数据的情况
        if (list.size() == 0) {
            System.out.println("没有该数据");
        }

        //表头数据
        System.out.println("id\t\t姓名\t年龄\t家庭住址");

        //遍历数据
        for (int i = 0; i < list.size(); i++) {
            student stu = list.get(i);
            System.out.println(stu.getId() + "\t" + stu.getName() + "\t" + stu.getAge() + "\t" + stu.getAddress());
        }
    }

    //查询id是否存在
    public static boolean contions(ArrayList<student> list,String id){
        //利用循环进行一个遍历数据
        for (int i = 0; i < list.size(); i++) {
            //拿到对象后进行判断
           student stu=list.get(i);
           String sid=stu.getId();
           //相等返回true  否则返回false
           if(sid.equals(id)){
               return true;
           }

        }
        return false;
    }
}
